<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
<!--          <el-form-item label="分类id" prop="flId">
            <el-input v-model="queryParams.flId" placeholder="请输入分类id" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>-->
          <el-form-item label="信息类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="请选择信息类型" clearable>
              <el-option
                v-for="dict in business_information_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人姓名" prop="lxrXm">
            <el-input v-model="queryParams.lxrXm" placeholder="请输入联系人姓名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="联系人电话" prop="lxrDh">
            <el-input v-model="queryParams.lxrDh" placeholder="请输入联系人电话" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="分类" prop="lxNameYj">
            <el-input v-model="queryParams.lxNameYj" placeholder="请输入分类" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
<!--          <el-form-item label="子分类" prop="lxNameEj">-->
<!--            <el-input v-model="queryParams.lxNameEj" placeholder="请输入子分类" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
          <el-form-item label="审核状态" prop="shzt">
            <el-select v-model="queryParams.shzt" placeholder="请选择审核状态" clearable>
              <el-option
                v-for="dict in business_information_zt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item label="发布人id" prop="userId">
            <el-input v-model="queryParams.userId" placeholder="请输入发布人id" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:information:add']">新增</el-button>
          </el-col>
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['business:information:edit']">修改</el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['business:information:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:information:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
<!--        <el-table-column label="" align="center" prop="id" v-if="true" />-->
<!--        <el-table-column label="分类id" align="center" prop="flId" />-->
        <el-table-column label="审核状态" align="center" prop="shzt" width="100px" :show-overflow-tooltip="true">
          <template #default="scope">
            <dict-tag :options="business_information_zt" :value="scope.row.shzt"/>
          </template>
        </el-table-column>
        <el-table-column label="信息类型" align="center" prop="type" width="100px" :show-overflow-tooltip="true">
          <template #default="scope">
            <dict-tag :options="business_information_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="联系人姓名" align="center" prop="lxrXm" width="100px" :show-overflow-tooltip="true"/>
        <el-table-column label="联系人电话" align="center" prop="lxrDh" width="100px" :show-overflow-tooltip="true"/>
        <el-table-column label="微信号" align="center" prop="wechat" width="150px" :show-overflow-tooltip="true"/>
        <el-table-column label="性别" align="center" prop="gender" width="100px" :show-overflow-tooltip="true"/>
        <el-table-column label="分类" align="center" prop="lxNameYj" width="120px" :show-overflow-tooltip="true"/>
<!--        <el-table-column label="子分类" align="center" prop="lxNameEj" />-->
<!--        <el-table-column label="内容" align="center" prop="nr" />-->
<!--        <el-table-column label="图片" align="center" prop="photo">-->
<!--          <template #default="scope">-->
<!--              <ImagePreview :width="100" :height="100" :src="scope.row.photoUrl"-->
<!--                            :preview-src-list="scope.row.photoUrl?.split(',')"/>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="地址" align="center" prop="dz" />-->
        <el-table-column label="出发地" align="center" prop="province" width="200px" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ scope.row.province + scope.row.city + scope.row.districts + scope.row.dz }}
          </template>
        </el-table-column>
        <el-table-column label="目的地" align="center" prop="city"  width="200px" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ scope.row.destinationProvince + scope.row.destinationCity + scope.row.destinationDistricts + scope.row.destinationAddress }}
          </template>
        </el-table-column>
        <el-table-column label="出发时间" align="center" prop="departureTime"  width="200px" :show-overflow-tooltip="true"/>
        <el-table-column label="空位数" align="center" prop="digit"  width="100px" :show-overflow-tooltip="true"/>
        <el-table-column label="乘车人数" align="center" prop="digit" width="100px" :show-overflow-tooltip="true"/>
        <el-table-column label="车型" align="center" prop="vehicleType" width="100px" :show-overflow-tooltip="true"/>
        <el-table-column label="备注" align="center" prop="nr" width="200px" :show-overflow-tooltip="true"/>
<!--        <el-table-column label="身份" align="center" prop="sf" />-->

        <el-table-column label="审核理由" align="center" prop="shly" width="200px" :show-overflow-tooltip="true"/>
<!--        <el-table-column label="发布人id" align="center" prop="userId" />-->
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
            <el-tooltip content="审核" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:information:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:information:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="50%" append-to-body>
      <el-form ref="informationFormRef" :model="form" :rules="rules" label-width="120px">
<!--        <el-form-item label="分类id" prop="flId">
          <el-input v-model="form.flId" placeholder="请输入分类id" />
        </el-form-item>-->
<!--        <el-form-item label="信息类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
                v-for="dict in business_information_type"
                :key="dict.value"
                :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="lxrXm">
          <el-input v-model="form.lxrXm" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="lxrDh">
          <el-input v-model="form.lxrDh" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="出发地" prop="area">
          <el-cascader v-model="form.area" :options="addressOptions"  :props="customProps" @change="handleChange" />
        </el-form-item>
        <el-form-item label="出发地详细地址" prop="dz">
          <el-input v-model="form.dz" placeholder="请输入出发地详细地址" />
        </el-form-item>
        <el-form-item label="目的地" prop="destinationArea">
          <el-cascader v-model="form.destinationArea" :options="addressOptions"  :props="customProps" @change="handleDestinationChange" />
        </el-form-item>
        <el-form-item label="目的地详细地址" prop="destinationAddress">
          <el-input v-model="form.destinationAddress" placeholder="请输入目的地详细地址" />
        </el-form-item>
        <el-form-item label="出发时间" prop="departureTime">
          <el-date-picker
            v-model="form.departureTime"
            type="datetime"
            placeholder="请选择出发时间"
            format="YYYY/MM/DD HH:mm"
          />
        </el-form-item>
        <el-form-item label="空位数" prop="digit">
          <el-input v-model="form.digit" placeholder="请输入空位数" />
        </el-form-item>
        <el-form-item label="乘车人数" prop="digit">
          <el-input v-model="form.digit" placeholder="请输入乘车人数" />
        </el-form-item>
        <el-form-item label="车型" prop="vehicleType">
          <el-input v-model="form.vehicleType" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="备注" prop="nr">
          <el-input v-model="form.nr" placeholder="请输入备注" />
        </el-form-item>-->
        <el-form-item label="审核状态" prop="shzt">
          <el-radio-group v-model="form.shzt">
            <el-radio
                v-for="dict in business_information_zt"
                :key="dict.value"
                :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核理由" prop="shly">
          <el-input v-model="form.shly" placeholder="请输入审核理由" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Information" lang="ts">
import { listInformation, getInformation, delInformation, addInformation, updateInformation,getAreaList } from '@/api/business/information';
import { InformationVO, InformationQuery, InformationForm } from '@/api/business/information/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { business_information_zt, business_information_type } = toRefs<any>(proxy?.useDict('business_information_zt', 'business_information_type'));

const informationList = ref<InformationVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const informationFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: InformationForm = {
  id: undefined,
  flId: undefined,
  type: undefined,
  bt: undefined,
  nr: undefined,
  photo: undefined,
  dz: undefined,
  sf: undefined,
  lxrXm: undefined,
  lxrDh: undefined,
  shzt: undefined,
  shly: undefined,
  userId: undefined,
  area:undefined,
  province:undefined,
  city: undefined,
  districts: undefined,
  departureTime: undefined,
  gender: undefined,
  wechat: undefined,
  destinationArea: undefined,
  destinationProvince: undefined,
  destinationCity: undefined,
  destinationDistricts: undefined,
  destinationAddress: undefined,
  digit: undefined,
  vehicleType: undefined,
  lxNameYj: undefined,
}
const data = reactive<PageData<InformationForm, InformationQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    flId: undefined,
    type: undefined,
    bt: undefined,
    nr: undefined,
    photo: undefined,
    dz: undefined,
    sf: undefined,
    lxrXm: undefined,
    lxrDh: undefined,
    shzt: undefined,
    shly: undefined,
    userId: undefined,
    params: {
    }
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listInformation(queryParams.value);
  informationList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 省市区 */
let addressOptions = ref([])
let customProps ={
  children: 'children',
  label: 'name',
  value: 'name',
}
const getAddress = async () => {
  const res = await getAreaList();
  addressOptions.value = res.data;
}
const handleChange = (value: any, selectedOptions: any) => {
  console.log(1);
  console.log(value);
  form.value.province = value[0].name;
  form.value.city = value[1].name;
  form.value.districts = value[2].name;
  form.value.area = value[2].name;
}
const handleDestinationChange = (value: any, selectedOptions: any) => {
  console.log(22222);
  console.log(value);
  form.value.destinationProvince = value[0].name;
  form.value.destinationCity = value[1].name;
  form.value.destinationDistricts = value[2].name;
  form.value.destinationArea = value[2].name;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  informationFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: InformationVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: InformationVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getInformation(_id);
  Object.assign(form.value, res.data);
  form.value.area = res.data.districts
  form.value.destinationArea = res.data.destinationDistricts
  dialog.visible = true;
  dialog.title = "修改信息";
}

/** 提交按钮 */
const submitForm = () => {
  informationFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateInformation(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addInformation(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: InformationVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delInformation(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/information/export', {
    ...queryParams.value
  }, `information_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
  getAddress();
});
</script>
